import { EnumBetResult, EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { RecmInfo, RoundInfo } from "../ExecutePlan";
import { IMoneyStrategy } from "../IMoneyStrategy";
import { BossRabbitMoneyStrategy, IBossRabbitMoneyStrategyOption } from "./BossRabitMoneyStrategy";
import { BossMoneyStrategyV3, IBossMoneyStrategyV2Option } from "./BossMoneyStrategyV2";

export class BossRabitRandomStepStrategy extends BossRabbitMoneyStrategy implements IMoneyStrategy {


    constructor(name: string, option?: IBossRabbitMoneyStrategyOption | null) {
        super(name, option);
    }

    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {

        let betResult = recmInfo.summaryInfo.recmResults;
        let prevResult = betResult[betResult.length - 1];
        let count = 0;
        if (prevResult == EnumBetResult.Success) {
            let pv = betResult[betResult.length - 2]
            while (pv == EnumBetResult.Fail) {
                count++;
                pv = betResult[betResult.length - 2 - count];
                if (count > 1) {
                    break;
                }
            }
            if (count > 1) {
                this.winStep = -2;
            }
            else {
                this.winStep = -1;
            }
        }

        let [recmV, recmMoney] = super.toMoney(roundInfo, recmInfo, userRecm, baseMoney);
        return [recmV, recmMoney]
    }
    showMoney(): [EnumBjlResult, number] {
        return [this.recmV, this.currentMoney];
    }
    resetStrategy(): void {
        super.resetStrategy();

    }
    strategyName: string = "";
    debugCompoenent: string = "";
    resetRecm(): void {

    }
    copyValue(valueObj: any): void {
        let obj = valueObj as BossRabitRandomStepStrategy;

        super.copyValue(obj);
    }

}

export class BossRabitRandomStepStrategyV2 extends BossMoneyStrategyV3 implements IMoneyStrategy {


    constructor(name: string, option?: IBossMoneyStrategyV2Option | null) {
        super(name, option);
    }

    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {

        let betResult = recmInfo.summaryInfo.recmResults;
        let prevResult = betResult[betResult.length - 1];
        let count = 0;
        if (prevResult == EnumBetResult.Success) {
            let pv = betResult[betResult.length - 2]
            while (pv == EnumBetResult.Fail) {
                count++;
                pv = betResult[betResult.length - 2 - count];
                if (count > 1) {
                    break;
                }
            }
            if (count > 1) {
                this.winStep = -2;
            }
            else {
                this.winStep = -1;
            }
        }

        let [recmV, recmMoney] = super.toMoney(roundInfo, recmInfo, userRecm, baseMoney);
        return [recmV, recmMoney]
    }
    showMoney(): [EnumBjlResult, number] {
        return [this.recmV, this.currentMoney];
    }
    resetStrategy(): void {
        super.resetStrategy();

    }
    strategyName: string = "";
    debugCompoenent: string = "";
    resetRecm(): void {

    }
    copyValue(valueObj: any): void {
        let obj = valueObj as BossRabitRandomStepStrategy;

        super.copyValue(obj);
    }

}


export class BossWinRabitRandomStepStrategy extends BossRabbitMoneyStrategy {


    constructor(name: string, option?: IBossRabbitMoneyStrategyOption | null) {
        super(name, option);
    }

    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {

        let betResult = recmInfo.summaryInfo.recmResults;
        if (recmInfo.summaryInfo.lwin % 2 == 0) {
            this.winStep = -3;
        }
        else {
            this.winStep = -1;
        }

        let [recmV, recmMoney] = super.toMoney(roundInfo, recmInfo, userRecm, baseMoney);
        return [recmV, recmMoney]
    }
    showMoney(): [EnumBjlResult, number] {
        return [this.recmV, this.currentMoney];
    }
    resetStrategy(): void {
        super.resetStrategy();

    }
    strategyName: string = "";
    debugCompoenent: string = "";
    resetRecm(): void {

    }
    copyValue(valueObj: any): void {
        let obj = valueObj as BossRabitRandomStepStrategy;

        super.copyValue(obj);
    }
}

export interface IBossWinAndFailStopRabitRandomStepOption extends IBossRabbitMoneyStrategyOption {
    stopCount: number;
    restartCount: number;
}

export class BossWinAndFailStopRabitRandomStepStrategy extends BossWinRabitRandomStepStrategy {
    stopCount: number = 3;
    restartCount: number = 2;
    runing: boolean = true;
    constructor(name: string, option?: IBossWinAndFailStopRabitRandomStepOption | null) {
        super(name, option);
        if (option) {
            this.stopCount = option.stopCount;
            this.restartCount = option.restartCount;
        }
        else {
            this.stopCount = 3;
            this.restartCount = 2;
        }
    }
    resetStrategy(): void {
        super.resetStrategy();
        this.runing = true;
    }
    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {

        let recmV = EnumBjlResult.U;
        let recmMoney = 0;
        [recmV, recmMoney] = super.toMoney(roundInfo, recmInfo, userRecm, baseMoney);

        if (this.runing && recmInfo.roundSummaryInfo.lfail >= this.stopCount) {
            this.runing = false;
            userRecm.roundSummaryInfo.resetSummaryInfo();
            recmInfo.roundSummaryInfo.resetSummaryInfo();
        }
        else if (!this.runing && userRecm.roundSummaryInfo.lwin >= this.restartCount) {
            this.runing = true;
        }
        if (!this.runing) {
            recmV = EnumBjlResult.U;
            recmMoney = 0;
        }

        return [recmV, recmMoney]
    }
    copyValue(valueObj: any): void {
        super.copyValue(valueObj);
        let obj = valueObj as BossWinAndFailStopRabitRandomStepStrategy;
        if (obj) {
            this.runing = obj.runing;
        }
    }
}